package com.attendance.www.hr.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.attendance.www.hr.beans.EmployeeBean;
import com.attendance.www.hr.beans.EmployeeCorporationBean;
import com.attendance.www.hr.beans.EmployeePageBean;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IEmployeeCorporationService;
import com.zhangkongli.frame.core.ResultMap;

/**
 * HR分配公司接口
 * @author 张鑫
 * 2017-11-07
 *
 */
@Controller
@RequestMapping("/employeeCorporation")
public class EmployeeCorporationController {
	
	@Resource
	private IEmployeeCorporationService employeeCorporationService;
	
	/**
	 * 修改HR的公司，新增和修改、删除共用的方法
	 */
	@RequestMapping(value = "/updateEmployeeCorporation.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object updateEmployeeCorporation(HttpServletRequest request,@RequestBody EmployeeCorporationBean employeeCorporationBean){
		if (employeeCorporationBean!=null) {
			if (employeeCorporationBean.getEmployeeId()==null
					||employeeCorporationBean.getCorporationBeanList()==null) {
				throw new ResultFaultException("0_00001");
			}
			if (employeeCorporationService.updateEmployeeCorporation(employeeCorporationBean)) {
				return ResultMap.success();
			}
		}
		throw new ResultFaultException("0_00008");
	}
	
	/**
	 * 查询员工公司分配信息
	 */
	@RequestMapping(value = "/getEmployeeCorporationList.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object getEmployeeCorporationList(HttpServletRequest request,@RequestBody EmployeeBean employeeBean){
		Integer pageNo=employeeBean.getPerPage();
		Integer pageCount=employeeBean.getRowCount();
		if (pageNo==null||pageCount==null) {
			throw new ResultFaultException("0_00001");
		}
		Integer startNum = 0;
		if (pageNo > 1) {
			startNum = (pageNo - 1) * pageCount; 
		}
		EmployeePageBean employeePageBean = new EmployeePageBean();
		employeePageBean = employeeCorporationService.getEmployeeCorporationList(employeeBean, startNum, pageCount);
		return ResultMap.success(employeePageBean);
	}
	/**
	 * 根据员工Id查询HR分配的公司
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getEmployeeCorporationListById.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object getEmployeeCorporationListById(HttpServletRequest request){
		String employeeId= request.getParameter("employeeId");
		if (StringUtils.isBlank(employeeId)) {
			throw new ResultFaultException("0_00001");
		}
		
		return ResultMap.success(employeeCorporationService.getEmployeeCorporationListById(employeeId));
	}
		
}
